package com.ruoyi.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.admin.mapper.*;
import com.ruoyi.admin.vo.MalfunctionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.domain.Malfunction;
import com.ruoyi.admin.service.IMalfunctionService;

/**
 * 故障信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@Service
public class MalfunctionServiceImpl implements IMalfunctionService
{
    @Autowired
    private MalfunctionMapper malfunctionMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private BedMapper bedMapper;
    /**
     * 查询故障信息
     *
     * @param malfunctionId 故障信息主键
     * @return 故障信息
     */
    @Override
    public Malfunction selectMalfunctionByMalfunctionId(Long malfunctionId)
    {
        return malfunctionMapper.selectMalfunctionByMalfunctionId(malfunctionId);
    }

    @Override
    public MalfunctionVo selectMalfunctionByMalfunctionId2(Long malfunctionId)
    {
        Malfunction malfunction = malfunctionMapper.selectMalfunctionByMalfunctionId(malfunctionId);
        String hospitalName = hospitalMapper.selectHospitalByHospitalId(malfunction.getHospitalId()).getHospitalName();
        String departmentName = departmentMapper.selectDepartmentByDeptId(malfunction.getDeptId()).getDepartmentName();
        MalfunctionVo malfunctionVo = new MalfunctionVo();
        malfunctionVo.setMalfunctionId(malfunction.getMalfunctionId());
        malfunctionVo.setDescribes(malfunction.getDescribes());
        malfunctionVo.setHospitalName(hospitalName);
        malfunctionVo.setTitle(malfunction.getTitle());
        malfunctionVo.setDepartmentName(departmentName);
        malfunctionVo.setStatue(malfunction.getStatue());
        malfunctionVo.setTime(malfunction.getTime());
        return malfunctionVo;
    }

    /**
     * 查询故障信息列表
     *
     * @param malfunction 故障信息
     * @return 故障信息
     */
    @Override
    public List<Malfunction> selectMalfunctionList(Malfunction malfunction)
    {
        return malfunctionMapper.selectMalfunctionList(malfunction);
    }


    @Override
    public List<MalfunctionVo> selectMalfunctionList2(Malfunction malfunction) {
        List<Malfunction> malfunctions = malfunctionMapper.selectMalfunctionList(malfunction);
        ArrayList<MalfunctionVo> list = new ArrayList<MalfunctionVo>();
        for ( Malfunction malfunction1 :  malfunctions ) {
            MalfunctionVo malfunctionVo = new MalfunctionVo();

            if(malfunction1.getHospitalId() != null){
                String hospitalName = hospitalMapper.selectHospitalByHospitalId(malfunction1.getHospitalId()).getHospitalName();
                malfunctionVo.setHospitalName(hospitalName);
            }
            if(malfunction1.getDeptId() != null){
                String departmentName = departmentMapper.selectDepartmentByDeptId(malfunction1.getDeptId()).getDepartmentName();
                malfunctionVo.setDepartmentName(departmentName);
            }
            if(malfunction1.getOrderId() != null){
                String orderNumber = orderMapper.selectOrderByOrderId(malfunction1.getOrderId()).getOrderNumber();
                malfunctionVo.setOrderNumber(orderNumber);
            }
            if (malfunction1.getBedId() != null){
                String bedCode = bedMapper.selectBedByBedId(malfunction1.getBedId()).getBedCode();
                malfunctionVo.setBedCode(bedCode);

            }
            malfunctionVo.setMalfunctionId(malfunction1.getMalfunctionId());
            malfunctionVo.setType(malfunction1.getType());
            malfunctionVo.setDescribes(malfunction1.getDescribes());
            malfunctionVo.setLoginId(malfunction1.getLoginId());
            malfunctionVo.setUserId(malfunction1.getUserId());
            malfunctionVo.setHospitalId(malfunction1.getHospitalId());
            malfunctionVo.setTitle(malfunction1.getTitle());
            malfunctionVo.setDeptId(malfunction1.getDeptId());
            malfunctionVo.setBedId(malfunction1.getBedId());
            malfunctionVo.setStatue(malfunction1.getStatue());
            malfunctionVo.setTime(malfunction1.getTime());
            malfunctionVo.setOrderId(malfunction1.getOrderId());



            list.add(malfunctionVo);
        }
        return list;
    }

    /**
     * 新增故障信息
     *
     * @param malfunction 故障信息
     * @return 结果
     */
    @Override
    public int insertMalfunction(Malfunction malfunction) {
        return malfunctionMapper.insertMalfunction(malfunction);
    }

    /**
     * 修改故障信息
     *
     * @param malfunction 故障信息
     * @return 结果
     */
    @Override
    public int updateMalfunction(Malfunction malfunction)
    {
        return malfunctionMapper.updateMalfunction(malfunction);
    }

    /**
     * 批量删除故障信息
     *
     * @param malfunctionIds 需要删除的故障信息主键
     * @return 结果
     */
    @Override
    public int deleteMalfunctionByMalfunctionIds(Long[] malfunctionIds)
    {
        return malfunctionMapper.deleteMalfunctionByMalfunctionIds(malfunctionIds);
    }

    /**
     * 删除故障信息信息
     *
     * @param malfunctionId 故障信息主键
     * @return 结果
     */
    @Override
    public int deleteMalfunctionByMalfunctionId(Long malfunctionId)
    {
        return malfunctionMapper.deleteMalfunctionByMalfunctionId(malfunctionId);
    }
}
